<?php
/*Problem Domain
To create a class that represents the List of Session class
*/

/*
- ListOfSessions is new ListOfSession(String , array)

- interp. new ListOfSession(sessionlist_name, list_sessions) represents the ListOfSession where
	- sessionlist_name is name given to that list of Sessions to attend by trainee
	- list_sessions is the actal array of Session class

- Example.
$los1 = new ListOfSessions('week1_sessions' , array($sess1 , $sess2));
$lost = new ListOfSessions('week2_sessions');

- Template
function fn-for-ListOfSessions()
{
	...$this->sessionlist_name;	//String
	...$this->list_sessions;
}
*/
class ListOfSessions
{
	private $sessionlist_name;
	private $list_sessions;

	/*String, array 
	purpose. initializes the newly created List Of Sessions*/
	public function __construct($sessionlist_name = null, $list_sessions = null)
	{
		$this->sessionlist_name = $sessionlist_name;
		$this->list_sessions 	= $list_sessions; 
	}
	
	/*void -> String
	purpose. produces the session List name for the current ListOfSessions object*/
	public function getSessionListName()
	{
		return $this->sessionlist_name;
	}

	/*String -> void
        purpose. sets the given session List name for the current ListOfSessions object*/
        public function setSessionListName($sessionlist_name)
        {
                $this->sessionlist_name = $sessionlist_name;
        }

	/*void -> ListOfSessions
        purpose. produces the List Of Sessions of the current User*/
        public function getListOfSessions()
        {
                return $this->list_sessions;
        }

        /*ListOfSession -> void
        purpose. sets the given List Of Sessions for the current User*/
        public function setListOfSessions(ListOfSessions $list_sessions)
        {
                $this->list_sessions = $list_sessions;
        }

	/*Session -> boolean
	purpose. adds the given Session to the current ListOfSessions and returns true on success , false otherwise.*/
	public function addToList($sess)
	{
		$status = true;

		if(!($this->list_sessions[] = $sess))
		{
			$status = false;
		} 

		return $status;
	}

	/*Session -> boolean
	purpose. produces true(position) of the given Session in the current List of Session if found,
	false(-1) otherwise*/
	private function getPosition($sess)
	{
		$pos = -1;

		for($i = 0 ; $i < count($this->list_sessions); $i++)
		{
			if($this->list_sessions[$i]->getSessionId() == $sess->getSessionId())
			{
				$pos = $i;
				break;
			}
		}

		return $pos;
	}

	/*Session -> boolean
        purpose. removes the given Session from the current ListOfSessions and returns true on success , false otherwise.*/
        public function removeFromList($sess)
	{
		$status = true;

		if(($pos = $this->getPosition($sess)) != -1)
		{
			if(!array_splice($this->list_sessions , $pos , 1))
			{
				$status = false;
			}
		}
		else
		{
			echo 'the given session not found';
			$status = false;
		}

		return $status;
	}

}
?>
